package com.yj.lottery_system.service.impl;

import com.yj.lottery_system.controller.param.CreatePrizeParam;
import com.yj.lottery_system.controller.param.PageParam;
import com.yj.lottery_system.dao.dataObject.PrizeDO;
import com.yj.lottery_system.dao.mapper.PrizeMapper;
import com.yj.lottery_system.service.IPictureService;
import com.yj.lottery_system.service.IPrizeService;
import com.yj.lottery_system.service.dto.PageListDTO;
import com.yj.lottery_system.service.dto.PrizeDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements IPrizeService {
    @Resource
    private IPictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    /**
     * 创建一个奖品
     * @param param 奖品属性
     * @param file 奖品图
     * @return 奖品id
     */
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile file) {
        //上传图片
        String fileName = pictureService.savePicture(file);
        //构造数据 存入数据库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setDescription(param.getDescription());
        prizeDO.setName(param.getPrizeName());
        prizeDO.setPrice(param.getPrice());
        prizeDO.setImageUrl(fileName);
        //调用dao
        prizeMapper.insert(prizeDO);


        return prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO>  findPrizeList(PageParam param) {
        //总量
        int total = prizeMapper.count();
        //查询当前页列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
        for(PrizeDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTOList.add(prizeDTO);
        }
        return new PageListDTO<>(total,prizeDTOList);
    }
}
